package com.xzj.admin.analysis.base.custom.ct;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.custom.common.CommonAnalysis;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

/**
 * @program: xzj-admin
 * @ClassName: GzAnalysisHandle
 * @description: CT云贵州解析方法
 * @author: XiaZunJun
 * @create: 2024-11-12 22:38
 */
public class GzAnalysisHandle extends CommonAbstractAnalysisHandle {

    static {
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.region.name(), CommonAnalysis::regionSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.host.name(), GzAnalysisHandle::hostSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.volume.name(), GzAnalysisHandle::volumeSub);
    }

    public GzAnalysisHandle(String cloudId,String cloudType) {
        super(cloudId, cloudType,new GzThemeSpecialAnalysisHandle(cloudId,cloudType));
    }

    @Override
    public void analysisBefore(JSONObject msgJson) {
        if(msgJson.containsKey(CommonEnums.RESOURCE_TYPE.network.name())){
            JSONObject network = msgJson.getJSONObject(CommonEnums.RESOURCE_TYPE.network.name());
            JSONArray content = network.getJSONObject("data").getJSONArray("content");
            network.put("content",content);
            msgJson.put(CommonEnums.RESOURCE_TYPE.network.name(),network);
        }
    }

    /**
     * 构建依托于主机 主机与集群关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void hostSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        JSONObject subJson = new JSONObject();
        String clusterId = row.getString(fieldName);
        subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, clusterId));
        subJson.put("hostId", parentId);
        subJson.put("clusterId", clusterId);
        subThemeRows.add(subJson);
    }
    /**
     * 构建依托于磁盘 磁盘与虚机关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void volumeSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        String[] split = fieldName.split(CommonEnums.JOIN.DEFAULT.getValue());
        if(split.length > 1){
            JSONArray vms = row.getJSONObject(split[0]).getJSONArray(split[1]);
            for(int i = 0; i < vms.size(); i++){
                JSONObject subJson = new JSONObject();
                String vmId = vms.getJSONObject(i).getString("vmId");
                subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, vmId));
                subJson.put("parentId", parentId);
                subJson.put("vmId", vmId);
                subThemeRows.add(subJson);
            }
        }
    }
}
